<template>
  <div class="my-container">
            <!-- 已登录状态 -->
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <van-image
            class="avatar"
            round
            fit="cover"
            :src="userInfo.photo"
          />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button round size="mini">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{ userInfo.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.follow_count }}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.like_count }}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!-- 未登录状态 -->
    <div class="header not-login"  v-else>
      <!-- 登录按钮  -->
      <div class="login-btn" @click="$router.push('/login')">
        <img src="~@/assets/mobile.png" class="mobile" alt="">
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <!-- 中格导航 -->
    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 底部消息通知等部分 -->
    <van-cell title="消息通知" is-link/>
    <van-cell title="小智同学" is-link/>
    <van-cell title="退出登录" class="lgout-cell" v-if="user" @click="lgout" clickable/>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  data () {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState('user', ['user'])
  },
  created () {
    if (this.user) {
      this.getUserInfo()
    }
  },
  methods: {
    ...mapMutations('user', ['removeUser']),
    // 退出操作
    lgout () {
      this.$dialog.confirm({
        title: '确认退出吗？'
      })
        .then(() => {
          // 确认调用
          // on confirm
          this.removeUser()
        })
        .catch(() => {
          // 取消调用
          // on cancel
        })
    },
    // 获取用户资料
    async getUserInfo () {
      try {
        const { data } = await getUserInfo()
        console.log(data)
        this.userInfo = data.data
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  .header {
    height: 361px;
    background: url("~@/assets/banner.png");
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      align-items: center;
      .mobile {
        width: 132px;
        height: 132px;
      }
      .text {
        font-size: 28px;
        color: #fff;
        margin-top: 15px;
      }
    }
  }
  .user-info {
    .base-info {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          margin-right: 23px;
          border: 4px solid #fff;
        }
        .name {
          color: #fff;
          font-size: 30px;
        }
      }
    }
    .data-stats {
      display: flex;
      height: 130px;
      .data-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          margin-bottom: 10px;
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .grid-nav {
    .grid-item {
      height: 141px;
      i.toutiao {
        font-size: 45px;
        color: #ff9d1d;
      }
      i.toutiao-shoucang {
        color: #eb5253;
      }
      span.text {
        font-size: 28px;
      }
    }
  }
  .lgout-cell {
    margin-top: 9px;
    text-align: center;
    color: #d86262;
  }
}
</style>
